import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import java.util.*;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.JTextPane;
import javax.swing.text.BadLocationException;

public class G extends JApplet implements ActionListener
{
	//---------------------------------------------
	// Needed to please eclipse for some reason
	//
	private static final long serialVersionUID = 1L;
	
	//--------------------------------------------
    // combined to create the output window
    //
	JTextPane output;
	JScrollPane scrollPane;
	
	//--------------------------------------------
    // controls declarations
    //
	private JPanel controls;
	private JTextField studentName;
	private JTextField schoolName;
	private JTextField major;
	private JTextField gpaRange;
	private JTextField semesterRange;
	private JComboBox degreeLevelBox;
	private JButton submit;

	//--------------------------------------------
    // various menu items
    //
	private JMenuBar mb;
	private JMenu methodOfSelection;
	private JMenuItem byStudent, listStudent, bySchool, listSchool;
	
	public void init()
	{		
		this.setSize(new Dimension(580, 820));
	}
	
	public G()
	{
		//-------------------------------------------
        //Create a the screen
		//
        JPanel screen = new JPanel(new BorderLayout());
        scrollPane = new JScrollPane();
        output = new JTextPane();
        scrollPane.getViewport().add(output);
        
        screen.setBackground(new Color(255, 255, 255));
        screen.setSize(new Dimension(320, 240));
        
        screen.add(scrollPane, BorderLayout.CENTER);
        
        setupMenu();
        setupControls();
        
        //-------------------------------------------
        // Set the menu bar and add the label to the content pane.
        //
        setLayout(new GridLayout(2, 1, 2, 2));
        
        getContentPane().add(screen);
        getContentPane().add(controls);
        
        //-------------------------------------------
        // Amusing introductory message goes here
        //
        printToScreen("v1.0 \n\n\n");
	}
	
	private void printToScreen(String text)
	{
		try
		{
			output.getDocument().insertString(output.getDocument().getLength(), text, null);
			output.setCaretPosition(output.getDocument().getLength());
		}
		catch (BadLocationException e)
		{
			e.printStackTrace();
		}
	}
	
	//-------------------------------------------
    // Creates all the buttons
    //
	private void setupControls()
	{
		controls = new JPanel();
		
		controls.setLayout(new BoxLayout(controls, BoxLayout.Y_AXIS));
		
		controls.add(mb, BorderLayout.PAGE_START);
		
		JPanel studentNamePanel = new JPanel();
		studentNamePanel.setBorder(BorderFactory.createTitledBorder("Student Name"));
		
		studentName = new JTextField(20);
		studentNamePanel.add(studentName);
		controls.add(studentNamePanel);
		
		JPanel schoolNamePanel = new JPanel();
		schoolNamePanel.setBorder(BorderFactory.createTitledBorder("School Name"));
		
		schoolName = new JTextField(20);
		schoolNamePanel.add(schoolName);
		controls.add(schoolNamePanel);
		
		JPanel majorPanel = new JPanel();
		majorPanel.setBorder(BorderFactory.createTitledBorder("Major"));
		
		major = new JTextField(20);
		majorPanel.add(major);
		controls.add(majorPanel);
		
		JPanel degreePanel = new JPanel();
		degreePanel.setBorder(BorderFactory.createTitledBorder("Degree Level"));
		
		String[] degreeStrings = { "Degree N/A", "BS", "MS", "PDH" };
		degreeLevelBox = new JComboBox(degreeStrings);
		degreeLevelBox.setSelectedIndex(0);
		degreeLevelBox.addActionListener(this);
		
		degreePanel.add(degreeLevelBox);
		controls.add(degreePanel);
		
		JPanel gpaPanel = new JPanel();
		gpaPanel.setBorder(BorderFactory.createTitledBorder("GPA range"));
		
		gpaRange = new JTextField(20);
		gpaPanel.add(gpaRange);
		controls.add(gpaPanel);
		
		JPanel semesterPanel = new JPanel();
		semesterPanel.setBorder(BorderFactory.createTitledBorder("Semester range"));
		
		semesterRange = new JTextField(20);
		semesterPanel.add(semesterRange);
		controls.add(semesterPanel);
		
		submit = new JButton("Search");
		controls.add(submit, BorderLayout.PAGE_END);
		submit.addActionListener(this);
	}
	
	//-------------------------------------------
    // Creates the main menu and all the submenus (god dammit that is not a typo [lol it doesn't recognize dammit or typo as words either])
    //
	private void setupMenu()
	{
		mb = new JMenuBar();
		
		methodOfSelection= new JMenu("Return Type");		
		
		byStudent = new JMenuItem("Student"); 
		listStudent = new JMenuItem("Student List"); 
		bySchool = new JMenuItem("School");
		listSchool = new JMenuItem("School List");
		
		byStudent.addActionListener(this);
		listStudent.addActionListener(this);
		
		methodOfSelection.add(byStudent);
		methodOfSelection.add(listStudent);
		methodOfSelection.add(bySchool);
		methodOfSelection.add(listSchool);
		
		mb.add(methodOfSelection);
	}

	public void actionPerformed(ActionEvent action) 
	{
		try
		{
			if (action.getSource() == submit)
			{
				
				printToScreen(studentName.getText() + "\n");
				printToScreen(schoolName.getText() + "\n");
				printToScreen(major.getText() + "\n");
				printToScreen((String)degreeLevelBox.getSelectedItem() + "\n");
				printToScreen(gpaRange.getText() + "\n");
				printToScreen(semesterRange.getText() + "\n");
				printToScreen("\n");
			}
			else if (action.getSource() == byStudent)
			{
				printToScreen("Searching for single student record.\n");
			}
			else if (action.getSource() == listStudent)
			{
				printToScreen("Searching for list of students.\n");
			}
		}
		catch (Exception e)
		{
			e.printStackTrace();
			return;
		}
	}
}
